package br.com.sgpv2.service.secretaria;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.sgpv2.controler.NegocioExeception;
import br.com.sgpv2.entity.organograma.Secretaria;
import br.com.sgpv2.entity.pessoa.Contrato;
import br.com.sgpv2.hibernate.secretaria.SecretariaRepository;

/**
 * Classe de serviço de Pessoa.
 */
@Service(value = "secretariaService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class SecretariaServiceImpl implements SecretariaService{
	
	
	private SecretariaRepository secretariaRepository;

	@Override
	public void salvar(Secretaria secretaria) {
		secretariaRepository.salvar(secretaria);
		
	}

	@Override
	@Transactional
	public void alterar(Secretaria secretaria) {
		secretariaRepository.alterar(secretaria);
		
	}

	@Override
	public List<Secretaria> listar() {
		return secretariaRepository.listarOrder("descricao");
	}

	@Override
	public void excluir(Secretaria secretaria) {
		secretariaRepository.excluir(secretaria);
	}

	@Override
	public Secretaria buscar(Integer pk) throws NegocioExeception {
		return secretariaRepository.buscar(pk);
	}

	public SecretariaRepository getSecretariaRepository() {
		return secretariaRepository;
	}

	@Autowired
	public void setSecretariaRepository(SecretariaRepository secretariaRepository) {
		this.secretariaRepository = secretariaRepository;
	}

	@Override
	public boolean validarExclusao(String idSecretaria) {
		List<Contrato> contratos = secretariaRepository.listarContratosPorSecretaria(Integer.parseInt(idSecretaria));
		if(contratos.size() > 0){
			return false;
		}else{
			return true;
		}
		
	}
	
	public List<Secretaria> consultarPorDescricao(String descricao) {
		if (StringUtils.isNotBlank(descricao)) {
			return secretariaRepository.consultarPorDescricao(descricao);
		} else {
			return listar();
		}
	}

}
